/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MistnostTabPane.java
 *
 * Created on 27.6.2012, 10:29:26
 */
package cz.worsik.vypocty.topeni.tepelneztraty.view.mistnosti;

import cz.worsik.utils.DoubleFormat;
import cz.worsik.vypocty.topeni.tepelneztraty.model.Mistnost;
import cz.worsik.vypocty.topeni.tepelneztraty.model.Stena;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JScrollPane;

/**
 *
 * @author 231260
 */
public final class MistnostTabPane extends javax.swing.JPanel {

	final int SIRKA = 795;
	final int VYSKA = 233;
	
	private StenyTable obsahTabulkaSteny;
	private OtvoryTable obsahTabulkaOtvory;
	
	/** Creates new form MistnostTabPane */
	public MistnostTabPane(Mistnost mistnost) {
		super();
		
		initComponents();
		nastavKomponenty(mistnost);
		nastavHodnoty();
		obsahTabulkaSteny.aktualizujTabulku();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        infoPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        editPodlazi = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        editTeplota = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        editSvetovaStrana = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel6 = new javax.swing.JLabel();
        editPlochaSten = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jSeparator4 = new javax.swing.JSeparator();
        jLabel9 = new javax.swing.JLabel();
        editTepelnaZtrata = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(800, 520));
        setMinimumSize(new java.awt.Dimension(800, 520));
        setPreferredSize(new java.awt.Dimension(800, 520));

        infoPanel.setBackground(new java.awt.Color(255, 255, 255));
        infoPanel.setMaximumSize(new java.awt.Dimension(800, 30));
        infoPanel.setMinimumSize(new java.awt.Dimension(800, 30));
        infoPanel.setPreferredSize(new java.awt.Dimension(800, 30));
        infoPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Podlaží:");
        jLabel1.setPreferredSize(new java.awt.Dimension(40, 30));
        infoPanel.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, -1, -1));

        editPodlazi.setFont(new java.awt.Font("Tahoma", 1, 11));
        editPodlazi.setText("1NP");
        editPodlazi.setPreferredSize(new java.awt.Dimension(30, 30));
        infoPanel.add(editPodlazi, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 0, -1, -1));

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator1.setPreferredSize(new java.awt.Dimension(2, 26));
        infoPanel.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(94, 2, -1, -1));

        jLabel2.setText("Teplota v místnosti:");
        jLabel2.setPreferredSize(new java.awt.Dimension(100, 30));
        infoPanel.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 0, -1, -1));

        editTeplota.setFont(new java.awt.Font("Tahoma", 1, 11));
        editTeplota.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        editTeplota.setText("20");
        editTeplota.setPreferredSize(new java.awt.Dimension(20, 30));
        infoPanel.add(editTeplota, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 0, -1, -1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("°C");
        jLabel3.setPreferredSize(new java.awt.Dimension(20, 30));
        infoPanel.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(235, 0, -1, -1));

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator2.setPreferredSize(new java.awt.Dimension(2, 26));
        infoPanel.add(jSeparator2, new org.netbeans.lib.awtextra.AbsoluteConstraints(259, 2, -1, -1));

        jLabel4.setText("Světová strana:");
        jLabel4.setPreferredSize(new java.awt.Dimension(80, 30));
        infoPanel.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(275, 0, -1, -1));

        editSvetovaStrana.setFont(new java.awt.Font("Tahoma", 1, 11));
        editSvetovaStrana.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        editSvetovaStrana.setText("Severo-východ");
        editSvetovaStrana.setPreferredSize(new java.awt.Dimension(90, 30));
        infoPanel.add(editSvetovaStrana, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 0, -1, -1));

        jSeparator3.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator3.setPreferredSize(new java.awt.Dimension(2, 26));
        infoPanel.add(jSeparator3, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 2, -1, -1));

        jLabel6.setText("Plocha stěn:");
        jLabel6.setPreferredSize(new java.awt.Dimension(60, 30));
        infoPanel.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(475, 0, -1, -1));

        editPlochaSten.setFont(new java.awt.Font("Tahoma", 1, 11));
        editPlochaSten.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        editPlochaSten.setText("155,34");
        editPlochaSten.setPreferredSize(new java.awt.Dimension(45, 30));
        infoPanel.add(editPlochaSten, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 0, -1, -1));

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel8.setText("<html>m<sup>2</sup></html>");
        jLabel8.setPreferredSize(new java.awt.Dimension(20, 26));
        infoPanel.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(590, 0, -1, -1));

        jSeparator4.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator4.setPreferredSize(new java.awt.Dimension(2, 26));
        infoPanel.add(jSeparator4, new org.netbeans.lib.awtextra.AbsoluteConstraints(620, 2, -1, -1));

        jLabel9.setText("Tepelná ztráta:");
        jLabel9.setPreferredSize(new java.awt.Dimension(80, 30));
        infoPanel.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(630, 0, -1, -1));

        editTepelnaZtrata.setFont(new java.awt.Font("Tahoma", 1, 11));
        editTepelnaZtrata.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        editTepelnaZtrata.setText("2456");
        editTepelnaZtrata.setPreferredSize(new java.awt.Dimension(50, 30));
        infoPanel.add(editTepelnaZtrata, new org.netbeans.lib.awtextra.AbsoluteConstraints(715, 0, -1, -1));

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel11.setText("W");
        jLabel11.setPreferredSize(new java.awt.Dimension(20, 30));
        infoPanel.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(770, 0, -1, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(infoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(infoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel editPlochaSten;
    private javax.swing.JLabel editPodlazi;
    private javax.swing.JLabel editSvetovaStrana;
    private javax.swing.JLabel editTepelnaZtrata;
    private javax.swing.JLabel editTeplota;
    private javax.swing.JPanel infoPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    // End of variables declaration//GEN-END:variables

	public void nastavHodnoty() 
	{
		Mistnost mistnost = getMistnost();
		
		if (mistnost == null)
			return;
		
		this.setName(mistnost.getNazevTabu());
		editPodlazi.setText(mistnost.getObjekt().getPodlaziList().get(mistnost.getPodlazi()));
		editTeplota.setText(""+mistnost.getTeplota_vnitrni());
		editSvetovaStrana.setText(mistnost.getSvetova_strana().getPopis());
		editPlochaSten.setText(DoubleFormat.trim2(mistnost.getPlocha_sten()));
		editTepelnaZtrata.setText(DoubleFormat.trim2(mistnost.getTepelna_ztrata()));
	}

	private void nastavKomponenty(Mistnost mistnost) 
	{
		JScrollPane obsahScrollPaneSteny;
		JScrollPane obsahScrollPaneOtvory;
		TTHeader tth;
		
		// Nastaveni tabulky sten
		obsahTabulkaSteny = new StenyTable(mistnost);
		obsahTabulkaSteny.addPopupListener(mistnost.getObjekt().getVychoziHodnoty());
		obsahScrollPaneSteny = new JScrollPane(obsahTabulkaSteny);
		this.add(obsahScrollPaneSteny);
		obsahScrollPaneSteny.setBounds(0, 30, SIRKA, VYSKA);
		obsahScrollPaneSteny.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		// popisky hlavicky
		tth = new TTHeader (obsahTabulkaSteny.getColumnModel ());
		tth.setToolTipsText (obsahTabulkaSteny.getModel().getPopiskyProToolTip());
		tth.setToolTipText ("Default ToolTip text");
		obsahTabulkaSteny.setTableHeader (tth);
		
		// Nastaveni tabulky otvoru
		obsahTabulkaOtvory = new OtvoryTable(obsahTabulkaSteny.getModel().getOtvoryModel());
		obsahTabulkaOtvory.addPopupListener();
		obsahScrollPaneOtvory = new JScrollPane(obsahTabulkaOtvory);
		this.add(obsahScrollPaneOtvory);
		obsahScrollPaneOtvory.setBounds(0, VYSKA+30, SIRKA, VYSKA);
		obsahScrollPaneOtvory.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		// popisky hlavicky
		tth = new TTHeader (obsahTabulkaOtvory.getColumnModel ());
		tth.setToolTipsText (obsahTabulkaOtvory.getModel().getPopiskyProToolTip());
		tth.setToolTipText ("Default ToolTip text");
		obsahTabulkaOtvory.setTableHeader (tth);
		
		obsahTabulkaSteny.getModel().addPropertyChangeListener(
			new PropertyChangeListener() {
				@Override
				public void propertyChange( PropertyChangeEvent evt ) {
					obsahTabulkaSteny.getModel().setSumy();
					nastavHodnoty();
				}
			} 
		);
		
		obsahTabulkaOtvory.getModel().addPropertyChangeListener(
			new PropertyChangeListener() {
				@Override
				public void propertyChange( PropertyChangeEvent evt ) {
					obsahTabulkaSteny.getModel().refresh(false);
					obsahTabulkaSteny.getModel().setSumy();
					nastavHodnoty();
				}
			} 
		);
	}
	
	public void showKomponenty()
	{
		
	}

	public Mistnost getMistnost() 
	{
		if (obsahTabulkaSteny == null)
			return null;
		return obsahTabulkaSteny.getModel().getMistnost();
	}
	
	public void addStena()
	{
		obsahTabulkaSteny.addStena();
	}
	
	public void removeStena()
	{
		obsahTabulkaSteny.removeStena();
	}
	
	public void addDvereDoVybraneSteny()
	{
		obsahTabulkaSteny.addDvere();
	}
	
	public void addOknoDoVybraneSteny()
	{
		obsahTabulkaSteny.addOkno();
	}
	
	public boolean isSelectedStena()
	{
		return obsahTabulkaSteny.getSelectedRow() >= 0;
	}
	
	public void setLastStenaSelected()
	{
		obsahTabulkaSteny.setRowSelectionInterval(obsahTabulkaSteny.getRowCount()-1, obsahTabulkaSteny.getRowCount()-1);
	}
	
	public void refreshTable()
	{
		obsahTabulkaSteny.getModel().refresh();
	}
}
